package com.bxj.wxtt.spider;

/**
 * 东方头条
 */

import com.bxj.wxtt.api.domain.AdvChannel;
import com.bxj.wxtt.api.domain.AdvSite;
import com.bxj.wxtt.api.service.IAdvChannelDubboService;
import com.bxj.wxtt.api.service.IAdvSiteDubboService;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.context.ApplicationContext;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.selector.Selectable;

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DongfangToutiaoPageProcessor implements ChannelPageProcessor {

    private Integer channelId = null;

    private Site site = Site.me().setRetryTimes(3).setSleepTime(100).me().setCharset("UTF-8").setUserAgent("Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");
    private String sourceName = null;
    private String channelName = null;
    private ApplicationContext applicationContext;

    public DongfangToutiaoPageProcessor() {

    }

    public static void taskScreenShot(WebDriver driver, File saveFile) {
        File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(src, saveFile);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    private void spiderNewsContent(String href, Map<String, String> map, ChromeDriver driver) {

        try {
            driver.get(href);
            taskScreenShot(driver, new File("capture.png"));
//            WebElement element = getElement(driver, By.xpath("//div[contains(@class, 'J-title_detail')]"));
            WebElement element = getElement(driver, By.xpath("//*[@id=\"title\"]/div[2]/span"));

            //*[@id="toutiao1"]/div[2]/ul/li[12]/span/a
            String a = element.getText();
            map.put("time", a);


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }

    public void process(Page page) {

        Map<String, List<String>> headers = new HashMap<>();
        headers.put("userAgent", Arrays.asList("Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"));
        page.setHeaders(headers);
        List<Map> list = new ArrayList<Map>();

        if (page.getUrl().get().contains("eastnewspc.dfshurufa.com")) {
            //[@class='news_nr_biaoti']
            List<Selectable> nodes = page.getHtml().xpath("//div[@id='toutiao1']/div[@class='news_nr']/ul/li").nodes();
            for (int i = 0; nodes != null && i < nodes.size(); i += 5) {
                Selectable element = nodes.get(i + 1);
                Selectable element2 = nodes.get(i + 2);
                Selectable element3 = nodes.get(i + 3);
                Selectable element4 = nodes.get(i + 4);

                Map<String, String> map = new HashMap<>();

                map.put("mode", "adv");
                map.put("channelId", String.valueOf(channelId));
                map.put("origin", channelName);
                map.put("type", "0");
                map.put("img", element3.xpath("div/html()").get());
                map.put("img1", element4.xpath("div/html()").get());

                String title = element.xpath("span/a/text()").get();
                map.put("title", title);

                String href = element.links().toString();
                map.put("href", href);

                String regEx = ".*mini\\.eastday\\.com\\/a\\/(.*)\\.html";
                // 编译正则表达式
                Pattern pattern = Pattern.compile(regEx);

                Matcher matcher = pattern.matcher(href);
                if (matcher.find() && matcher.groupCount() > 0) {
                    String id = matcher.group(1);
                    System.out.println(id);
                    map.put("id", id);
                }
                page.addTargetRequests(Arrays.asList(href));

                list.add(map);
            }

        } else {

            Map<String, String> map = new HashMap<>();
            map.put("mode", "content");
            map.put("channelId", String.valueOf(channelId));

            String regEx = ".*mini\\.eastday\\.com\\/a\\/([^-]*)[-]*(.*)\\.html";
            // 编译正则表达式
            Pattern pattern = Pattern.compile(regEx);
            String id = "";
            Matcher matcher = pattern.matcher(page.getUrl().get());
            if (matcher.find() && matcher.groupCount() > 1) {
                id = matcher.group(1);
                String num = matcher.group(2);
                System.out.println(id);
                map.put("id", id);
                map.put("num", num.isEmpty() ? "1" : num);


            }
            getRecommand(id, page, list);
            String time = page.getHtml().xpath("//div[@class='J-title_detail title_detail]/div[@class='share_cnt_p clearfix']/div/i/html()").get();
            map.put("time", time);
            String content = page.getHtml().xpath("//div[@id='J-contain_detail_cnt']/html()").get();
            map.put("content", content);

            List<Selectable> paginations = page.getHtml().xpath("//div[@class='pagination']").xpath("a").nodes();
            for (Selectable pagination : paginations) {
                if (!pagination.links().get().contains(id))
                    continue;
                if (pagination.toString().contains("下一页"))
                    continue;
                page.addTargetRequest(pagination.links().get());
            }
            list.add(map);
        }
        page.putField("list", list);
    }

    private void getRecommand(String recommand_id, Page page, List<Map> list) {

        List<Selectable> recommands = page.getHtml().xpath("//div[@id='hot_recommend_cnt']/ul/li[@class='recommend_news']/a[@class='red_news']").nodes();
        for (Selectable recommand : recommands) {
            Map<String, String> map = new HashMap<>();
            map.put("mode", "relation");
            map.put("recommand_id", recommand_id);
            map.put("channelId", String.valueOf(channelId));
            map.put("origin", channelName);
            map.put("type", "0");
            map.put("img", recommand.xpath("img/@data-url").get());
            map.put("title", recommand.xpath("h3/text()").get());

            String href = recommand.links().toString();
            map.put("href", href);

            String regEx = ".*mini\\.eastday\\.com\\/a\\/(.*)\\.html.*";
            // 编译正则表达式
            Pattern pattern = Pattern.compile(regEx);

            Matcher matcher = pattern.matcher(href);
            if (matcher.find() && matcher.groupCount() > 0) {
                String id = matcher.group(1);
                System.out.println(id);
                map.put("id", id);
            }
            page.addTargetRequests(Arrays.asList(href));

            list.add(map);
        }
    }
//
//    private List<WebElement> getElements(WebDriver driver, By by) {
//        try {
//            return driver.findElements(by);
//        } catch (Exception e) {
//            return null;
//        }
//    }

    private WebElement getElement(WebDriver driver, By by) {
        try {
            return driver.findElement(by);
        } catch (Exception e) {
            return null;
        }
    }


    @Override
    public Site getSite() {
        return site;
    }


    //    @Override

    public static void main(String[] args) {
        String regEx = ".*mini\\.eastday\\.com\\/a\\/([^-]*)[-]*(.*)\\.html";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regEx);

        Matcher matcher = pattern.matcher("https://mini.eastday.com/a/180717042345448.html");
        if (matcher.find() && matcher.groupCount() > 0) {
            String a = matcher.group(1);
            String b = matcher.group(2);
            System.out.println(a);
        }
    }

    /**
     * 设置来源站点
     */
    @Override
    public void setSiteName(String name) {
        this.sourceName = name;
    }

    /**
     * 设置频道Id
     */
    @Override
    public void setChannelId(Integer channelId) {
        this.channelId = channelId;
    }

    /**
     * 设置上下文
     *
     * @param applicationContext
     */
    @Override
    public void setContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
        IAdvChannelDubboService advChannelDubboService = applicationContext.getBean(IAdvChannelDubboService.class);
        IAdvSiteDubboService advSiteDubboService = applicationContext.getBean(IAdvSiteDubboService.class);
        AdvChannel advChannel = advChannelDubboService.selectById(channelId);
        AdvSite advSite = advSiteDubboService.selectById(advChannel.getAdvSiteId());
        channelName = advSite.getName() + advChannel.getName() + "频道";

    }
}